package com.yaozheng.vocationaltraining.fragment;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.ListView;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.IntegralDetailsAdapter;
import com.yaozheng.vocationaltraining.adapter.MemoryWealthAdapter;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.iview.ITokenBaseView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.IntegralDetailsService;
import com.yaozheng.vocationaltraining.service.impl.IntegralDetailsServiceImpl;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

/**
 * 积分明细列表
 *
 * @author Administrator
 */
@EFragment(R.layout.fragment_integral_details)
public class IntegralDetailsFragment extends BaseFragment implements
        ITokenBaseView, OnRefreshListener, ILoadMoreView {
    @ViewById
    ListView listView;

    @ViewById
    TextView headTitleText;

    /**
     * 下拉刷新
     */
    @ViewById
    SwipeRefreshLayout swipeRefreshLayout;

    @Bean(IntegralDetailsServiceImpl.class)
    IntegralDetailsService integralDetailsService;

    IntegralDetailsAdapter integralDetailsAdapter;

    int lastId;
    ListViewLoadMoreManage listViewLoadMoreManage;

    String title;
    /**
     * 0 积分详情 1 经验详情
     */
    int type;

    public void setTitle(String title) {
        this.title = title;
    }

    public void setType(int type) {
        this.type = type;
    }

    @AfterViews
    public void initView() {
        headTitleText.setText(title);
        integralDetailsService.init(this);

//        swipeRefreshLayout.postDelayed(new Runnable() {
//
//            @Override
//            public void run() {
//                swipeRefreshLayout.setRefreshing(true);
//                onRefresh();
//            }
//        }, 500);

        onRefresh();
        swipeRefreshLayout.setOnRefreshListener(this);
        listViewLoadMoreManage = new ListViewLoadMoreManage(this,
                getBaseActivity(), listView);
    }

    @UiThread
    @Override
    public void responseSuccess(JSONObject jsonObject) {
        cancelLoadDataProgressDialog();
        JSONArray coursesDataList = TypeUtils.getJsonArray(jsonObject, "data",
                "list");
        if (coursesDataList != null) {
            if (integralDetailsAdapter == null) {
                integralDetailsAdapter = new IntegralDetailsAdapter(
                        getBaseActivity(), coursesDataList, type);
                listView.setAdapter(integralDetailsAdapter);

            } else {
                if (lastId == 0) {
                    integralDetailsAdapter.getDataList().clear();
                }
                integralDetailsAdapter.getDataList().addAll(coursesDataList);
                integralDetailsAdapter.notifyDataSetChanged();
            }
        }
    }

    @UiThread
    @Override
    public void responseError(String error) {
        cancelLoadDataProgressDialog();
        alertMessage(error);
    }

    @Override
    public void cancelLoadDataProgressDialog() {
        swipeRefreshLayout.setRefreshing(false);
        listViewLoadMoreManage.loadMoreComplete();
    }

    @Override
    public void onRefresh() {
        lastId = 0;
        loadData();
    }

    /**
     * 加载数据
     */
    public void loadData() {
        integralDetailsService.integralDetailsList(type, lastId);

    }

    @Override
    public void loadMoreData() {
        if (integralDetailsAdapter != null) {
            int lastId = integralDetailsAdapter.getLastId();
            if (lastId > 0) {
                this.lastId = lastId;
                loadData();
            }
        }
    }

    @Override
    public boolean isResponseResult() {
        return isAdded();
    }

    @Override
    public void onDestroyView() {
        integralDetailsAdapter = null;
        super.onDestroyView();
    }

}
